//
//  JXGuideController.m
//  JiaoX
//
//  Created by 尹桥印 on 14/11/16.
//  Copyright (c) 2014年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXGuideController.h"
#import "POPCustomAnimation.h"
#import "GuideTitleView.h"
#import "POPSpringAnimation.h"
#import "GuideTextView.h"
#import "POPBasicAnimation.h"
#import "JXTabBarController.h"

// 将角度转换为弧度
#define DEGREES__TO__RADIANS(d)  ((d) * M_PI / 180.f)

#define POINT_INTERVAL 40
#define SECTION_INTERVAL [DimensUtils getScaleY:50]
#define ENTER_BTN_FONT_SIZE [DimensUtils getScaleX:18]

@interface JXGuideController ()<UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
    NSArray *_section1Points;
    NSArray *_section2Points;
    NSArray *_section3Points;
    
    NSMutableArray *_pointViews;
    NSMutableArray *_imageArray;
    NSMutableDictionary *_besideViews;//边上的view
    
    CGFloat _globalYInset;
    
    CGFloat DOANIM_Y;//执行动画的y值
    
    CGFloat historyY;
    
    NSInteger _currentIndex;
}

@end

@implementation JXGuideController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:JXGlobalColorBg];
    
    [UserDefaultsHelper setObject:[Utils getAppVersion] forKey:KEY_APP_VERSION];
    
    //第1步，隐藏状态栏
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    //第2步，初始化array
    [self initArray];
    
    _globalYInset = JXScreenH/2;
    
    
    
    //添加scrollview
    _scrollView = [[UIScrollView alloc]init];
    _scrollView.backgroundColor = [UIColor whiteColor];
    _scrollView.frame = CGRectMake(self.view.bounds.origin.x, JXScreenH/2, self.view.bounds.size.width, self.view.bounds.size.height);
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    // 3.设置其他属性
    _scrollView.contentSize = CGSizeMake(0, 5000);
    _scrollView.showsVerticalScrollIndicator = NO;
    

    
    [self initArray];
    [self addSection1Content];
    [self addSection2Content];
    [self addSection3Content];
    [self addEnterBtn];
    
    
    [UIView animateWithDuration:0.5 animations:^{
        _scrollView.frame = self.view.bounds;
    } completion:^(BOOL finished) {
        [self showFirstView];
    }];
}

-(void)initArray{
    _section1Points = [[NSArray alloc] initWithObjects:
                       @"guide_point_0",@"guide_point_0",@"guide_point_1",
                       @"guide_point_0",@"guide_point_0",@"guide_point_2",
                       @"guide_point_0",@"guide_point_0",@"guide_point_3",
                       @"guide_point_0",@"guide_point_0",@"guide_point_4",
                       @"guide_point_0",@"guide_point_0",@"guide_point_0",nil];
    
    _section2Points = [[NSArray alloc] initWithObjects:
                       @"guide_point_0",@"guide_point_0",@"guide_point_0",@"guide_point_5",@"guide_point_0",
                       @"guide_point_0",@"guide_point_0",@"guide_point_0",@"guide_point_6",@"guide_point_0",
                       @"guide_point_0",@"guide_point_0",@"guide_point_0",nil];
    
    _section3Points = [[NSArray alloc] initWithObjects:
                       @"guide_point_0",@"guide_point_0",@"guide_point_0",@"guide_point_0",@"guide_point_7",
                       @"guide_point_0",@"guide_point_0",@"guide_point_0",@"guide_point_0",@"guide_point_0",
                       @"guide_point_0",@"guide_point_8",nil];
    
    _imageArray = [NSMutableArray array];
    _pointViews = [NSMutableArray array];
    _besideViews = [NSMutableDictionary dictionary];
}

-(void)showFirstView{
    UIImageView *view = [_imageArray firstObject];
    [self doScaleSizeAnim:view isZoomIn:YES];
    _currentIndex ++;
}

#pragma mark - 场景导购

-(void)addSection1Content{
    //先添加顶部title
    GuideTitleView *guide1 = [[GuideTitleView alloc]initWithTitle:@"场景导购" subTitle:@"真实情爱场景化导购体验\r\n带你调情带你飞"];
    _globalYInset = 30;
//    _globalYInset = JXScreenH/2-guide1.height/2;
    guide1.y = _globalYInset;
    [_scrollView addSubview:guide1];
    _globalYInset = CGRectGetMaxY(guide1.frame)+POINT_INTERVAL;
    
    //添加小点
    for (int i=0; i<_section1Points.count; i++) {
        UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:_section1Points[i]]];
        imgView.frame = CGRectMake((JXScreenW-imgView.width)/2, i*POINT_INTERVAL+_globalYInset, imgView.width, imgView.height);
        [_scrollView addSubview:imgView];
        [_pointViews addObject:imgView];
        if (i==5) {
            //如果i＝3；
//            DOANIM_Y = imgView.y;
            DOANIM_Y = JXScreenH/3*2;
        }
    }
    
    _globalYInset = CGRectGetMaxY(((UIView *)[_pointViews lastObject]).frame)+SECTION_INTERVAL;
    
    [self addCJDGText:@"guide_icon_1" index:2 title:@"在这儿，爱得更久" subTitle:@"摘下“快男”标签" type:GuideTextViewTypeRight];
    [self addCJDGText:@"guide_icon_2" index:5 title:@"在这儿，更加紧俏" subTitle:@"跟“松下”说拜拜" type:GuideTextViewTypeLeft];
    [self addCJDGText:@"guide_icon_3" index:8 title:@"在这儿，释放激情" subTitle:@"撸Sir也有春天" type:GuideTextViewTypeRight];
    [self addCJDGText:@"guide_icon_4" index:11 title:@"在这儿，唤醒欲望" subTitle:@"做自己的高潮女王" type:GuideTextViewTypeLeft];
}

/**
 *  添加场景导购上面的小图标
 *
 *  @param imageName
 */
-(void)addCJDGText:(NSString *)imageName index:(NSUInteger)index title:(NSString *)title subTitle:(NSString *)subTitle type:(GuideTextViewType)type{
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
    [imageView setHidden:YES];
    [imageView setSize:CGSizeMake([DimensUtils getScaleY:imageView.width], [DimensUtils getScaleY:imageView.height])];
    imageView.center = ((UIView *)_pointViews[index]).center;
    [imageView setTag:index];
    [_scrollView addSubview:imageView];
    [_imageArray addObject:imageView];
    
    GuideTextView *textView = [[GuideTextView alloc]initWithTitle:title subTitle:subTitle type:type];
    textView.y =((UIView *)_pointViews[index]).center.y - textView.height/2;
    if (type == GuideTextViewTypeRight) {
        textView.x = CGRectGetMaxX(imageView.frame)+JXPublicInset/2;
    }else{
        textView.x = CGRectGetMinX(imageView.frame)-JXPublicInset/2-textView.width;
    }
    [textView setAlpha:0.0];
    [_besideViews setObject:textView forKey:[NSNumber numberWithLong:index]];
    [_scrollView addSubview:textView];
    
}

#pragma mark - 达人交流
-(void)addSection2Content{
    //先添加顶部title
    GuideTitleView *guide2 = [[GuideTitleView alloc]initWithTitle:@"达人交流" subTitle:@"与众多情趣达人交流分享\r\n叫我情趣大湿"];
    guide2.y = _globalYInset;
    [_scrollView addSubview:guide2];
    _globalYInset = CGRectGetMaxY(guide2.frame)+POINT_INTERVAL;
    
    //添加小点
    for (int i=0; i<_section2Points.count; i++) {
        UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:_section2Points[i]]];
        imgView.frame = CGRectMake((JXScreenW-imgView.width)/2, i*POINT_INTERVAL+_globalYInset, imgView.width, imgView.height);
        [_scrollView addSubview:imgView];
        [_pointViews addObject:imgView];
    }
    _globalYInset = CGRectGetMaxY(((UIView *)[_pointViews lastObject]).frame)+SECTION_INTERVAL;
    
    [self addDRJLView:@"guide_icon_5" index:18 besideImageName:@"guide_point_5_flow" type:GuideTextViewTypeRight];
    [self addDRJLView:@"guide_icon_6" index:23 besideImageName:@"guide_point_6_flow" type:GuideTextViewTypeLeft];
    
}

-(void)addDRJLView:(NSString *)imageName index:(NSUInteger)index besideImageName:(NSString *)besideName type:(GuideTextViewType)type{
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
    [imageView setHidden:YES];
    [imageView setSize:CGSizeMake([DimensUtils getScaleX:imageView.width], [DimensUtils getScaleX:imageView.height])];
    imageView.center = ((UIView *)_pointViews[index]).center;
    [imageView setTag:index];
    [_scrollView addSubview:imageView];
    [_imageArray addObject:imageView];
    
    
    UIImageView *besideImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:besideName]];
    [besideImage setAlpha:0.0];
    besideImage.y = ((UIView *)_pointViews[index]).center.y - besideImage.height;
    if (type == GuideTextViewTypeRight) {
        besideImage.x = CGRectGetMaxX(imageView.frame)-JXPublicInset;
    }else{
        besideImage.x = CGRectGetMinX(imageView.frame)+JXPublicInset-besideImage.width;
    }
    [_besideViews setObject:besideImage forKey:[NSNumber numberWithLong:index]];
    [_scrollView addSubview:besideImage];
}

#pragma mark - 品类齐全

-(void)addSection3Content{
    //先添加顶部title
    GuideTitleView *guide3 = [[GuideTitleView alloc]initWithTitle:@"品类齐全" subTitle:@"万千情趣品类一应俱全\r\n我有我有我全都有"];
    guide3.y = _globalYInset;
    [_scrollView addSubview:guide3];
    _globalYInset = CGRectGetMaxY(guide3.frame)+POINT_INTERVAL;
    
    //添加小点
    for (int i=0; i<_section3Points.count; i++) {
        UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:_section3Points[i]]];
        imgView.frame = CGRectMake((JXScreenW-imgView.width)/2, i*POINT_INTERVAL+_globalYInset, imgView.width, imgView.height);
        [_scrollView addSubview:imgView];
        [_pointViews addObject:imgView];
    }
    _globalYInset = CGRectGetMaxY(((UIView *)[_pointViews lastObject]).frame)+SECTION_INTERVAL;
    
    [self addPLQQView:@"guide_icon_7" index:32];
    [self addPLQQView:@"guide_icon_8" index:39];
    
    _globalYInset = _globalYInset + ((UIView *)[_imageArray lastObject]).height/2+40;
}

-(void)addPLQQView:(NSString *)imageName index:(NSUInteger)index{
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
    [img setHidden:YES];
    [img setSize:CGSizeMake([DimensUtils getScaleY:img.width], [DimensUtils getScaleY:img.height])];
    img.center = ((UIView *)_pointViews[index]).center;
    [_scrollView addSubview:img];
    [_imageArray addObject:img];
}

-(void)addEnterBtn{
    UIButton *btn = [[UIButton alloc]init];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:ENTER_BTN_FONT_SIZE]];
    [btn setBackgroundImage:[UIImage resizedImage:@"btn_login_bg"] forState:UIControlStateNormal];
    [btn setTitle:@"开启娇羞之门" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setFrame:CGRectMake(JXPublicInset, _globalYInset, JXScreenW-JXPublicInset*2, 47)];
    [btn addTarget:self action:@selector(enterBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:btn];
    
    [_scrollView setContentSize:CGSizeMake(JXScreenW, CGRectGetMaxY(btn.frame)+50)];
}

-(void)enterBtnClick:(UIButton *)btn{
    //去首页
    //显示状态栏
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[JXTabBarController alloc]init];
    [window makeKeyAndVisible];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y>historyY) {
        //向上滑
        if (_currentIndex < _imageArray.count) {
            // 往上滑,展示下面的东西
            UIImageView *image = _imageArray[_currentIndex];
            UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
            CGRect rect=[image convertRect: image.bounds toView:window];
            if (rect.origin.y < DOANIM_Y) {
                [self doScaleSizeAnim:image isZoomIn:YES];
                _currentIndex++;
            }
        }
    } else if (scrollView.contentOffset.y<historyY) {
        //向下滑
        if (_currentIndex - 1 >= 0) {
            UIImageView *image = _imageArray[_currentIndex - 1];
            UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
            CGRect rect=[image convertRect: image.bounds toView:window];
            if (rect.origin.y+rect.size.height/2 > DOANIM_Y) {
                [self doScaleSizeAnim:image isZoomIn:NO];
                _currentIndex--;
            }
        }
    }
    historyY = scrollView.contentOffset.y;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    historyY = scrollView.contentOffset.y;
}

/**
 *  进行缩放动画
 *
 *  @param view   imageview
 *  @param zoomIn 是否是放大，YES 代表放大 NO 代表缩小
 */
-(void)doScaleSizeAnim:(UIImageView *)view isZoomIn:(BOOL)zoomIn{
    [view setHidden:NO];
    [view.layer pop_removeAllAnimations];
    
    POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerSize];
    springAnimation.springBounciness = 20.0;
    springAnimation.springSpeed = 20.0;
    if(zoomIn){
        springAnimation.fromValue = [NSValue valueWithCGSize:CGSizeZero];
        //放大
        springAnimation.toValue = [NSValue valueWithCGSize:view.image.size];
    }else{
        springAnimation.fromValue = [NSValue valueWithCGSize:view.image.size];
        //缩小
        springAnimation.toValue = [NSValue valueWithCGSize:CGSizeZero];
    }

    [view.layer pop_addAnimation:springAnimation forKey:@"changesize"];
    
    //取出旁边的view。去执行旋转与透明度的动画
    UIView *besideView = [_besideViews objectForKey:[NSNumber numberWithLong:view.tag]];
    if(besideView){
        [self doRotationYAnim:besideView isShow:zoomIn];
    }
}

-(void)doRotationYAnim:(UIView *)view isShow:(BOOL)show{
    [view.layer pop_removeAllAnimations];
    view.layer.transform = CATransform3DIdentity;
    //旋转动画
    POPBasicAnimation *rotationAnim = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerRotationY];
    rotationAnim.fromValue = @(DEGREES__TO__RADIANS(0));
    rotationAnim.toValue = @(DEGREES__TO__RADIANS(360));
    [view.layer pop_addAnimation:rotationAnim forKey:kPOPLayerRotationY];
    [rotationAnim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        view.layer.transform = CATransform3DIdentity;
    }];
    
    //透明度动画
    POPBasicAnimation *alphaAnim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    alphaAnim.fromValue = @(0.0);
    alphaAnim.toValue = @(1.0);
    if (!show) {
        alphaAnim.fromValue = @(1.0);
        alphaAnim.toValue = @(0.0);
    }
    [view pop_addAnimation:alphaAnim forKey:kPOPViewAlpha];
}



@end
